﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MySparrow
{
    public partial class Form2 : Form
    {
        ShrinkArea _shrinkPanel;
        Point mousePoint;
        Point formPoint;

        const int  WM_LBUTTONDOWN = 0x0201;

        bool _showLabel = false;
        Point _labelPosition = new Point();

        public Form2()
        {
            InitializeComponent();
            this.BackColor = Color.FromArgb(84, 121, 67);
            this.TransparencyKey = Color.FromArgb(84, 121, 67);
            this.panel1.BackColor = Color.FromArgb(84, 121, 67);
            this.panel2.BackColor = Color.FromArgb(84, 121, 67);
            this.panel3.BackColor = Color.FromArgb(84, 121, 67);
            this.panel4.BackColor = Color.FromArgb(84, 121, 67);
            this.panel5.BackColor = Color.FromArgb(84, 121, 67);
            this.toolStrip1.BackColor = Color.FromArgb(84, 121, 67);

            _shrinkPanel = new ShrinkArea(this, 329, 841,ShrinkDirection.Horizontal);

            _shrinkPanel.Expanded = false;
            this.btnOpenPanel.Enabled = true;
            this.btnOpenPanel.BackgroundImage = global::MySparrow.Properties.Resources.openPanel;
            this.btnClosePanel.Enabled = false;
            this.btnClosePanel.BackgroundImage = global::MySparrow.Properties.Resources.closePanel;

            //this.dataGridView1.Columns.Add("Image", "");
            //this.dataGridView1.Columns["Image"].ValueType = typeof(System.Drawing.Image);
            //this.dataGridView1.Rows.Add(1);

            this.dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
            
            //this.dataGridView1.Rows[0].Cells[0].Value = global::MySparrow.Properties.Resources.openPanel as Image;


            //this.listView1.Items.Add(lvt2);

        }

        void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Icon")
            {
                e.Value = global::MySparrow.Properties.Resources.mailStar as Image;
            }
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Content")
            {
                
            }
               
        }

        void mailGrid_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >=0)
            {
                Image starImg = global::MySparrow.Properties.Resources.mailStar;
                //e.Graphics.DrawImage(starImg, new Point((e.CellBounds.Left + this.mailGrid.Columns[e.ColumnIndex].Width / 2 - starImg.Width /2) , (e.CellBounds.Top + this.mailGrid.RowTemplate.Height / 2 - starImg.Height / 2)));
            }
            e.Handled = true;

            

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void btnOpenPanel_Click(object sender, EventArgs e)
        {
            this.btnOpenPanel.Enabled = false;
            this.btnOpenPanel.BackgroundImage = global::MySparrow.Properties.Resources.openPanel2;
            this.btnClosePanel.Enabled = true;
            this.btnClosePanel.BackgroundImage = global::MySparrow.Properties.Resources.closePanel2;
            _shrinkPanel.Toggle();
        }

        private void btnClosePanel_Click(object sender, EventArgs e)
        {
            this.btnOpenPanel.Enabled = true;
            this.btnOpenPanel.BackgroundImage = global::MySparrow.Properties.Resources.openPanel;
            this.btnClosePanel.Enabled = false;
            this.btnClosePanel.BackgroundImage = global::MySparrow.Properties.Resources.closePanel;
            _shrinkPanel.Toggle();
        }

        private void panel4_MouseDown(object sender, MouseEventArgs e)
        {
            mousePoint = Control.MousePosition;
            formPoint = this.Location;
        }

        private void panel4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
            }
        }

        private void panel5_MouseDown(object sender, MouseEventArgs e)
        {
            mousePoint = Control.MousePosition;
            formPoint = this.Location;
        }

        private void panel5_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                this.Location = new Point(mousePos.X - mousePoint.X + formPoint.X, mousePos.Y - mousePoint.Y + formPoint.Y);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            this.pictureBox1.Image = global::MySparrow.Properties.Resources.close;
        }

        private void pictureBox2_MouseHover(object sender, EventArgs e)
        {
            this.pictureBox2.Image = global::MySparrow.Properties.Resources.Mini;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox1.Image = global::MySparrow.Properties.Resources.close1;
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox2.Image = global::MySparrow.Properties.Resources.Mini1;
        }

        private void pictureBox3_MouseHover(object sender, EventArgs e)
        {
            this.pictureBox3.Image = global::MySparrow.Properties.Resources.Label2;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            this.pictureBox3.Image = global::MySparrow.Properties.Resources.Label1;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            int labelListY = panel3.Location.Y - lvLabels.Size.Height + pictureBox3.Size.Height / 2;
            int labelListX = pictureBox3.Size.Width / 2;
            lvLabels.Location = new Point(labelListX,labelListY);
            lvLabels.Visible = true;
            this._showLabel = true;
            this._labelPosition = lvLabels.Location;
            lvLabels.Focus();
        }


        protected override void WndProc(ref Message m)
        {
            if (this._showLabel)
            {
                if (m.WParam.ToInt32() == WM_LBUTTONDOWN)
                {
                    Rectangle labelRect = new Rectangle(this._labelPosition, this.lvLabels.Size);

                    if (
                           ! labelRect.Contains(
                                                new Point((m.LParam.ToInt32() & 0xFFFF),(m.LParam.ToInt32() >> 16))
                                              )
                       )
                    {
                        this.lvLabels.Visible = false;
                        this._showLabel = false;
                    }
                }
            }

            base.WndProc(ref m);
        }

        private void mailGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                
            }
            
        }



    }
}
